package com.camundademo.rest.controller;


import com.camundademo.rest.dto.Results;
import com.camundademo.rest.service.CamundaProcessService;
import org.camunda.bpm.engine.rest.dto.repository.ProcessDefinitionDto;
import org.camunda.bpm.engine.rest.dto.repository.ProcessDefinitionQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 流程相关的
 */
@RestController//ResponseBody需要和 @Controller结合使用  RestController的每个方法都是按有ResponseBody执行的  就是出参处理不一样 入参处理是一样的
@RequestMapping("/processCtrl")
public class CamundaProcessCtrl {
    /**
     * 仓库服务,流程部署
     */
    @Autowired
    private CamundaProcessService camundaProcessService;

    /**
     * 查询流程定义列表
     * 参考部署的代码实现的
     * sortBy; 字段名
     * sortOrder; desc/asc
     */
    @PostMapping(value = "/queryProcessDefinition",
                 name = "查询流程定义列表",
                 consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Results<List<ProcessDefinitionDto>> queryProcessDefinition(
            ProcessDefinitionQueryDto processDefinitionQueryDto,
            @RequestParam(required = false)
            Integer firstResult,
            @RequestParam(required = false)
            Integer maxResults
                                                                     ) throws IOException {
        //参数校验完成送入服务层
        List<ProcessDefinitionDto> processDefinitionDtos = camundaProcessService.queryProcessDefinition(
                processDefinitionQueryDto, firstResult, maxResults);

        //包装返回值
        return Results.ok(processDefinitionDtos);
    }
}
